package com.guoxin.chapter4.reentractlock.conditiontestmanytomany;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyService {
	private Lock lock = new ReentrantLock();
	private Condition condition = lock.newCondition();
	private boolean hasValue = false;
	public void add(){
		try {
			lock.lock();
			while(hasValue){
				condition.await();
			}
			System.out.println(Thread.currentThread().getName()+" 打印★");
			hasValue = true;
			condition.signalAll();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}finally {
			lock.unlock();
		}
	}
	
	public void get(){
		try {
			lock.lock();
			while(!hasValue){
				condition.await();
			}
			System.out.println(Thread.currentThread().getName()+" 打印☆");
			hasValue = false;
			condition.signalAll();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}finally{
			lock.unlock();
		}
		
	}
}
